home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / misc / unix / tracker_4_3.lzh / tracker / Linux / audio.c next >
Encoding:
C/C++ Source or Header  |  1994-02-13  |  3.5 KB  |  166 lines

  1. /* linux/audio.c 
  2.     vi:se ts=3 sw=3:
  3.  */
  4. /* minor mods for pl14 by Mike Battersby */
  5. /* Modified from soundblaster_audio.c by Hannu Savolainen */
  6. /* hsavolai@cs.helsinki.fi */
  7.  
  8. #include "defs.h"
  9. #ifdef MALLOC_NOT_IN_STDLIB
  10. #include <malloc.h>
  11. #else
  12. #include <stdlib.h>
  13. #endif
  14. #include <stdio.h>
  15. #include <unistd.h>
  16. #include <fcntl.h>
  17. #include "extern.h"
  18.  
  19. #ifdef PL_14
  20. /* For some reason my pl14 kernel had no sys/soundcard.h (???) */
  21. #include "/usr/src/linux/drivers/sound/soundcard.h"
  22. #else
  23. #ifndef __386BSD__
  24. /*    This should be sys/soundcard.h    */
  25. #include <sys/soundcard.h>
  26. #else
  27. #include <machine/soundcard.h>
  28. #endif
  29. #endif
  30.  
  31. ID("$Id: audio.c,v 4.0 1994/01/11 17:59:57 espie Exp espie $")
  32.  
  33. LOCAL unsigned char *buffer;    /* buffer for ready-to-play samples */
  34. LOCAL short *buffer16;            /* Sure this isn't unsigned short ? */
  35. LOCAL int buf_index;               /* index conflicts with index(3) */
  36. LOCAL int buf_max;
  37. LOCAL int audio;               /* /dev/dsp */
  38.  
  39.  
  40. LOCAL int stereo;                    /* are we playing stereo or not ? */
  41. /* 256th of primary/secondary source for that side. */
  42. LOCAL int primary=512, secondary=0;
  43. LOCAL int dsp_samplesize = 16; /* must be 8 or 16 */
  44.  
  45. void set_mix(percent)
  46. int percent;
  47.     {
  48.     percent *= 256;
  49.     percent /= 100;
  50.     primary = percent;
  51.     secondary = 512 - percent;
  52.     }
  53.  
  54. int open_audio(f, s)
  55. int f;
  56. int s;
  57.     {
  58.     audio = open("/dev/dsp", O_WRONLY, 0);
  59.     if (audio == -1)
  60.         end_all("Error opening audio device");
  61.  
  62.     if (ioctl(audio, SNDCTL_DSP_SAMPLESIZE, &dsp_samplesize) == -1)
  63.         end_all("Error setting sample size");
  64.  
  65.     stereo = s;
  66.  
  67.     if (ioctl(audio, SNDCTL_DSP_STEREO, &stereo) == -1)
  68.         end_all("Error setting stereo/mono");
  69.  
  70.     if (f==0) f = 44100;
  71.  
  72.     if (ioctl(audio, SNDCTL_DSP_SPEED, &f) == -1)
  73.         end_all("Error setting frequency");
  74.  
  75.     if (ioctl (audio, SNDCTL_DSP_GETBLKSIZE, &buf_max) == -1)
  76.       end_all("Error getting buffsize");
  77.  
  78.     buffer = malloc(buf_max);
  79.     buffer16 = (short *)buffer;
  80.     buf_index = 0;
  81.  
  82.     return f;
  83.     }
  84.  
  85. LOCAL void actually_flush_buffer()
  86.     {
  87.     int l,i;
  88.  
  89.     l = sizeof(*buffer) * buf_index;
  90.     if (dsp_samplesize !=8) l *= 2;
  91.     write(audio, buffer, l);
  92.  
  93.     buf_index = 0;
  94.     }
  95.  
  96. void output_samples(left, right)
  97. int left, right;
  98.     {
  99.     if (dsp_samplesize != 8)    /* Cool! 16 bits/sample */
  100.     {
  101.         if (stereo)
  102.             {
  103.             if (buf_index * 2 >= buf_max - 1) 
  104.                actually_flush_buffer();
  105.  
  106.             buffer16[buf_index++] = 
  107.                ((left*primary + right*secondary) / 256);
  108.             buffer16[buf_index++] = 
  109.                ((right*primary + left*secondary) / 256);
  110.             }
  111.         else
  112.             {
  113.             if (buf_index * 2 >= buf_max) 
  114.                actually_flush_buffer();
  115.             buffer16[buf_index++] = (left + right);
  116.             }
  117.     }
  118.     else
  119.     {
  120.         if (stereo)
  121.             {
  122.             if (buf_index >= buf_max - 1) 
  123.                 actually_flush_buffer();
  124.             buffer[buf_index++] = ((left*primary + right*secondary) >> 16)
  125.                  + 128;
  126.             buffer[buf_index++] = ((right*primary + left*secondary) >> 16)
  127.                  + 128;
  128.             }
  129.         else
  130.             {
  131.             if (buf_index >= buf_max) 
  132.                 actually_flush_buffer();
  133.             buffer[buf_index++] = ((left + right) >> 8) + 128;
  134.             }
  135.         }
  136.     }
  137.  
  138. void flush_buffer()
  139.     {    /* Dummy version */
  140.     }
  141.  
  142. /*
  143.  * Closing the Linux sound device waits for all pending samples to play.
  144.  */
  145. void close_audio()
  146.     {
  147.     actually_flush_buffer();
  148.     close(audio);
  149.     free(buffer);
  150.     }
  151.  
  152. /* dummy system calls, to patch ? */
  153. void set_synchro(s)
  154.     {
  155.     }
  156.  
  157. int update_frequency()
  158.     {
  159.     return 0;
  160.     }
  161.  
  162. void discard_buffer()
  163.     {
  164.     }
  165.  
  166.